home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / libg++ / libio / filedoalloc.c < prev    next >
C/C++ Source or Header  |  1994-02-15  |  4KB  |  108 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*
  26.  * Copyright (c) 1990 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42. /* Modified for GNU iostream by Per Bothner 1991, 1992. */
  43.  
  44. #define _POSIX_SOURCE
  45. #include "libioP.h"
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48.  
  49. /* If this function pointer is non-zero, we should call it.
  50.    It's supposed to make sure _IO_flush_all gets called on exit.
  51.    We call it from _IO_file_doallocate, since that is likely
  52.    to get called by any program that does buffered I/O. */
  53. void (*_IO_cleanup_registration_needed)();
  54.  
  55. /*
  56.  * Allocate a file buffer, or switch to unbuffered I/O.
  57.  * Per the ANSI C standard, ALL tty devices default to line buffered.
  58.  *
  59.  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
  60.  * optimisation) right after the _fstat() that finds the buffer size.
  61.  */
  62.  
  63. int
  64. _IO_file_doallocate(fp)
  65.      register _IO_FILE *fp;
  66. {
  67.   register _IO_size_t size;
  68.   int couldbetty;
  69.   register char *p;
  70.   struct stat st;
  71.  
  72.   if (_IO_cleanup_registration_needed)
  73.     (*_IO_cleanup_registration_needed)();
  74.   
  75.   if (fp->_fileno < 0 || fp->_jumps->__stat(fp, &st) < 0)
  76.     {
  77.       couldbetty = 0;
  78.       size = _IO_BUFSIZ;
  79. #if 0
  80.       /* do not try to optimise fseek() */
  81.       fp->_flags |= __SNPT;
  82. #endif
  83.     }
  84.   else
  85.     {
  86.       couldbetty = S_ISCHR(st.st_mode);
  87. #if _IO_HAVE_ST_BLKSIZE
  88.       size = st.st_blksize <= 0 ? _IO_BUFSIZ : st.st_blksize;
  89. #else
  90.       size = _IO_BUFSIZ;
  91. #endif
  92.     }
  93. #ifdef USE_MALLOC_BUF
  94.   if ((p = malloc(size)) == NULL) {
  95.     unbuffered(1); /* FIXME */
  96.     /*    fp->_bf._IO_buf_base = fp->_p = fp->_nbuf; */
  97.     /*     fp->_bf._size = 1; */
  98.     return EOF;
  99.   }
  100. #else
  101.   p = ALLOC_BUF(size);
  102. #endif
  103.   _IO_setb(fp, p, p+size, 1);
  104.   if (couldbetty && isatty(fp->_fileno))
  105.     fp->_flags |= _IO_LINE_BUF;
  106.   return 1;
  107. }
  108.